package com.pzr.soccer.service.game.impl;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.pzr.soccer.domain.game.Game;
import com.pzr.soccer.domain.game.GameWrapper;
import lombok.Data;
import org.jsoup.nodes.Element;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Data
class ExtractorToday {

    ExtractorToday(Element tr, String baseUrl, boolean isNextDay_, LocalTime pre_game_begin_) {
        this.isNextDay=isNextDay_;
        this.pre_game_begin=pre_game_begin_;
        currentTime = tr.selectFirst("td.text-center.match_status > span.match_status_minutes").text();
        home = tr.selectFirst(" td.text-right > a").text();
        score = tr.selectFirst("td.text-center.match_goal").text();
        corner = tr.selectFirst("td.text-center.match_corner > div > span.span_match_corner").text();
        halfCorner = tr.selectFirst("td.text-center.match_corner > div > span.span_half_corner").text();
        danger = tr.selectFirst("td.text-center.match_attach > div.match_dangerous_attacks_div").text();
        shot = tr.selectFirst("td.text-center.match_shoot > div.match_shoot_div").text();
        stateUrl = baseUrl + tr.selectFirst("td.text-center.td_analysis > a:nth-child(3)").attr("href");
        league = tr.selectFirst("td.text-center.td_league > div > a").text();
        kick_off = tr.selectFirst("td:nth-child(3)").text();
        away = tr.selectFirst("td.text-left.match_away > a > span").text();
        yaPan = tr.selectFirst("td.text-center.match_handicap").text();
        daxiaoPan = tr.selectFirst("td.text-center.total_goals").text();

        String hyc=tr.selectFirst("td.text-right.match_home > span.yellow_card").text();
        String hrc = tr.selectFirst("td.text-right.match_home > span.red_card").text();
        String ayc = tr.selectFirst("td.text-left.match_away > span.yellow_card").text();
        String arc = tr.selectFirst("td.text-left.match_away > span.red_card").text();

        homeYellowCard=hyc.trim().isBlank()?0:Integer.parseInt(hyc);
        homeRedCard = hrc.trim().isBlank()?0:Integer.parseInt(hrc);
        awayYellowCard=ayc.trim().isBlank()?0:Integer.parseInt(ayc);
        awayRedCard=arc.trim().isBlank()?0:Integer.parseInt(arc);

        //未开始
        if (currentTime.isBlank() || currentTime.equals("0") ||currentTime.equals("00")) {
            gameCycle= GameWrapper.Game_Cycle_NotStartYet;
        }else {
            gameCycle= GameWrapper.Game_Cycle_Going;
        }

        //比赛是否跨日
        LocalTime kick_off_time = LocalTime.parse(kick_off);
        if(!this.isNextDay){
            //上场比赛比本场还晚2小时以上，证明跨日
            if (this.pre_game_begin.getHour() - kick_off_time.getHour() > 2) {
                this.isNextDay=true;
            }else {
               this.pre_game_begin = kick_off_time;
            }
        }
        actualKickOff =this.isNextDay? LocalDateTime.of(LocalDate.now().plusDays(1), kick_off_time)
                :LocalDateTime.of(LocalDate.now(), kick_off_time);
    }

    /*
      直接extract
     */
    String currentTime;
    String home;
    String score;
    String corner;
    String danger;
    String shot;
    String stateUrl;
    String league;
    String kick_off;
    String away;
    String yaPan;
    String daxiaoPan;
    String halfCorner;
    int homeYellowCard;
    int homeRedCard;
    int awayYellowCard;
    int awayRedCard;

    boolean isNextDay;
    LocalTime pre_game_begin;

    /*
      计算出的
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    LocalDateTime actualKickOff;
    int gameCycle;


    GameWrapper buildWrapper(){
        Game game = new Game();
        game.setId(actualKickOff.toLocalDate() + "_" + home);
        game.setHome(home);
        game.setLeague(league);
        game.setKickOff(actualKickOff);
        game.setAway(away);
        game.setYanPan(yaPan);
        game.setDaXiaoPan(daxiaoPan);

        GameWrapper wrapper = new GameWrapper();
        wrapper.setId(home);
        wrapper.setGame(game);
        wrapper.setGameCycle(gameCycle);
        if (yaPan.contains("+")) {
            game.setStrongTeam("away");
        } else {
            game.setStrongTeam("home");
        }

        return wrapper;
    }
}
